import Cookies from 'js-cookie';

// 本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地
// 本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地
// 本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地
// var ip = 'http://qzls-scs.natapp1.cc/' // 本地本地本地本地本地本地本地本地本地本地本地本地本地本地
// 本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地
// 本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地
// 本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地
// 本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地本地

// var address = '116.63.174.78'; // 线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上线上
var address = '192.168.2.154'; 



var webSocketDk = ':7397/'
export var webSocketIp = address + webSocketDk

var apiDk = ':8888/'
export var ip = 'http://' + address + apiDk

export const post = (that, methodName, url, para, callback, callback1) => {
  var projectID = Cookies.get('projectID')
  // var projectID = '7982116069111349248'
  if (projectID != null && projectID != undefined && projectID != '') {
    if (methodName == 'POST') {
      var headers = {
        'Content-type': 'application/json',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token'),
        'projectId': projectID
      }
    } else if (methodName == 'GET') {
      var headers = {
        'Content-type': 'application/json',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token'),
        'projectId': projectID
      }
    } else if (methodName == 'POST_x-www-form-urlencoded') {
      var headers = {
        'Content-type': 'application/x-www-form-urlencoded',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token'),
        'projectId': projectID
      }
    } else if (methodName == 'POST_multipart/form-data') {
      var headers = {
        // 'Content-type': 'multipart/form-data',
        // 'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token'),
        'projectId': projectID
      }
    } else if (methodName == 'POST_projectId') {
      var headers = {
        'projectId': '',
        'Content-type': 'application/json',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token'),
        'projectId': projectID
      }
    }
  } else {
    if (methodName == 'POST') {
      var headers = {
        'Content-type': 'application/json',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token')
      }
    } else if (methodName == 'GET') {
      var headers = {
        'Content-type': 'application/json',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token')
      }
    } else if (methodName == 'POST_x-www-form-urlencoded') {
      var headers = {
        'Content-type': 'application/x-www-form-urlencoded',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token')
      }
    } else if (methodName == 'POST_multipart/form-data') {
      var headers = {
        // 'Content-type': 'multipart/form-data',
        // 'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token')
      }
    } else if (methodName == 'POST_projectId') {
      var headers = {
        'projectId': '',
        'Content-type': 'application/json',
        'charset': 'utf-8',
        'Auth-Token': Cookies.get('Admin-Token')
      }
    }
  }
  if (methodName == 'POST') {
    that.$http({
      method: methodName,
      url: ip + url,
      data: para,
      headers: headers
    }).then(function(res) {
      if (res.data.code == 203) {
        that.$store.dispatch('user/logout').then(() => {
          that.$router.push('/login')
          that.$message.error('登录失效，请重新登录')
          // that.$router.go(0);
        })
      }
      callback(res.data)
    }).catch(function(err) {
      callback1(err)
    })
  } else if (methodName == 'GET') {
    var paraString = '?'
    for (var i in para) {
      paraString += i + '=' + para[i] + '&'
    }
    if (paraString != '') {
      paraString = paraString.substring(0, paraString.length - 1)
    }
    that.$http({
      method: methodName,
      url: ip + url + paraString,
      headers: headers
    }).then(function(res) {
      if (res.data.code == 203) {
        that.$store.dispatch('user/logout').then(() => {
          that.$router.push('/login')
          that.$message.error('登录失效，请重新登录')
          // that.$router.go(0);
        })
      }
      callback(res.data)
    }).catch(function(err) {
      callback1(err)
    })
  } else if (methodName == 'POST_x-www-form-urlencoded') {
    var paraString = ''
    for (var k in para) {
      paraString += k + '=' + para[k] + '&'
    }
    if (paraString != '') {
      paraString = paraString.substring(0, paraString.length - 1)
    }
    that.$http({
      method: 'POST',
      url: ip + url,
      data: paraString,
      headers: headers
    }).then(function(res) {
      if (res.data.code == 203) {
        that.$store.dispatch('user/logout').then(() => {
          that.$router.push('/login')
          that.$message.error('登录失效，请重新登录')
          // that.$router.go(0);
        })
      }
      callback(res.data)
    }).catch(function(err) {
      callback1(err)
    })
  } else if (methodName == 'POST_multipart/form-data') {
    var formData = new FormData()
    for (var Key in para) {
      if (para[Key] != null) {
        if (Array.isArray(para[Key])) {
          for (var i = 0; i < para[Key].length; i++) {
            formData.append(Key, para[Key][i])
          }
        } else if (typeof para[Key] === 'object' && para[Key].constructor != File) {
          for (var Key2 in para[Key]) {
            if (Key2 != 'myName') {
              formData.append(Key + '.' + Key2, para[Key][Key2])
            }
          }
        } else {
          formData.append(Key, para[Key])
        }
      }
    }
    that.$http({
      method: 'POST',
      url: ip + url,
      data: formData,
      headers: headers
    }).then(function(res) {
      if (res.data.code == 203) {
        that.$store.dispatch('user/logout').then(() => {
          that.$router.push('/login')
          that.$message.error('登录失效，请重新登录')
          // that.$router.go(0);
        })
      }
      callback(res.data)
    }).catch(function(err) {
      callback1(err)
    })
  } else if (methodName == 'POST_projectId') {
    that.$http({
      method: 'POST',
      url: ip + url,
      data: para,
      headers: headers
    }).then(function(res) {
      if (res.data.code == 203) {
        that.$message.warning(res.data.msg)
        that.$store.dispatch('user/logout').then(() => {
          that.$router.push('/login')
          that.$message.error('登录失效，请重新登录')
          // that.$router.go(0);
        })
      }
      callback(res.data)
    }).catch(function(err) {
      callback1(err)
    })
  }
}
